#! /usr/bin/env python
# -*- coding: utf-8 -*-
#Author:pako
#Email:zealzpc@gmail.com
"""
http://chart.apis.google.com/chart
   ?chxl=1:|周1|周2|周三|周四|周五
   &chxp=1,1,2,3,4,5
   &chxr=0,0,5000|1,0,10
   &chxs=0,676767,11.5,0,l,0000FF|1,676767,11.5,0,lt,676767
   &chxt=y,x
   &chs=640x320
   &cht=lxy
   &chco=49188F,00FF00
   &chds=0,10,0,5000,0,10,0,5000
   &chd=t:1,2,3,4,5|1608,273,87,111,200|1,2,3,4,5|100,300,250,1000,1500
   &chdl=iphone4|叶梓萱
   &chdlp=b
   &chls=1,24,0|1
   &chma=5,5,5,25|5,20
   &chtt=weibo
"""

def createMap(para):
#    templet="""
#http://chart.apis.google.com/chart
#   ?chxl=1:%(xAxesStr)s
#   &chxp=1,%(xAxesStrPosition)s
#   &chxr=0,%(yStartRange)d,%(yEndRange)d|1,%(xStartRange)d,%(xEndRange)d
#   &chxs=0,676767,11.5,0,l,0000FF|1,676767,11.5,0,lt,676767
#   &chxt=y,x
#   &chs=640x320
#   &cht=lxy
#   &chco=49188F,00FF00
#   &chds=%(chds)s
#   &chd=t:%(data)s
#   &chdl=%(data_label)s
#   &chdlp=b
#   &chls=1,24,0|1
#   &chma=5,5,5,25|5,20
#   &chtt=%(title)s
#"""
    templet=u"""http://chart.apis.google.com/chart?chxl=1:%(xAxesStr)s&chxp=1,%(xAxesStrPosition)s&chxr=0,%(yStartRange)d,%(yEndRange)d|1,%(xStartRange)d,%(xEndRange)d&chxs=0,676767,11.5,0,l,0000FF|1,676767,11.5,0,lt,676767&chxt=y,x&chs=%(chs)s&cht=lxy&chco=49188F,00FF00&chds=%(chds)s&chd=t:%(data)s&chdl=%(data_label)s&chdlp=b&chls=1,24,0|1&chma=5,5,5,25|5,20&chtt=%(title)s"""
    para_temp={
          "xAxesStr":"|周1|周2|周三|周四|周五",
          "xAxesStrPosition":'1,2,3,4,5',
          "yStartRange":0,
          "yEndRange":5000,
          "xStartRange":0,
          "xEndRange":10,
          "chds":"0,10,0,5000,0,10,0,5000",
          "data":'1,2,3,4,5|1608,273,87,111,200|1,2,3,4,5|100,300,250,1000,1500',
          "data_label":"iphone4|叶梓萱",
#          "title":'weibo hot word trend'
          "title":'weibo hot word trend'          
          }
    xAxesStr='|'+'|'.join(para[0]['xAxasString'])
#    print 'xAxesStr:',xAxesStr
    xAxesStrPosition=','.join([str(v) for v in range(len(para[0]['xAxasString']))])
#    print "xAxesStrPosition:",xAxesStrPosition
    yEndRange=max([int(y) for y in para[1]['yAxasData']])
#    print "yEndRange:",yEndRange
    xEndRange=len(para[0]['xAxasString'])
#    print "xEndRange:",xEndRange
    chds='0,%(xEndRange)d,0,%(yEndRange)d,0,%(xEndRange)d,0,%(yEndRange)d'%{"yEndRange":yEndRange,"xEndRange":xEndRange}
#    print 'chds:',chds
    data = xAxesStrPosition+'|'+','.join(para[0]['yAxasData'])+'|'+xAxesStrPosition+'|'+','.join(para[1]['yAxasData'])
#    print 'data :',data 
    data_label=para[0]['title']+'|'+para[1]['title']
#    print 'data_label:',data_label
    para_temp['xAxesStr']=xAxesStr
    para_temp['xAxesStrPosition']=xAxesStrPosition
    para_temp['yEndRange']=yEndRange
    para_temp['xEndRange']=xEndRange
    para_temp['chds']=chds
    para_temp['data']=data
    para_temp['data_label']=data_label
    para_temp['title']=para[0]['pic_title']
    search_date_count=len(para[0]['xAxasString'])
#    if search_date_count>15 and search_date_count <40:
#        para_temp['chs']=str(search_date_count*40)+'x400'
#    else:
    para_temp['chs']='1000x300'
#    print 'chs:',para_temp['chs']
    pic_url=templet%para_temp
#    print para_temp
#    print "pic_url:",pic_url
    return pic_url

if __name__=='__main__':
    para1=[
          {
           'title':'iphone4_original',
           'xAxasString':['mon','tue','thr','four','fif'],
           'yAxasData':['100','300','700','500','1600'],
           'pic_title':'luxiaobai'
           },
           {
           'title':'叶梓萱',
           'xAxasString':['mon','tue','thr','four','fif'],
           'yAxasData':['1000','1500','2000','5000','3500']
           }
          ]
    res=createMap(para1)
    print res
    print type(res)
    